
package monitorandwebserver;

import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author rafal
 */
public class ClientWriterThread extends Thread{
    
    ArrayList<VirtualConnection> virtualConnections;
    boolean work= true;
    SensorsData sensorsData;
    
    public ClientWriterThread(){
        virtualConnections = new ArrayList<VirtualConnection>();
        sensorsData = SensorsData.getInstance();
    }
    
    //TODO check if synchronized run and other methods will work when run is sleep(500)
    
    @Override
    public void run(){
        try{
        Socket socket;
        //infinite loop - send data to registered users every 0.5 seconds
        while(work){
            this.sleep(500);
            for (VirtualConnection virtualConnection : virtualConnections) {
                socket = virtualConnection.clientSocket;
                //will work only for established connections
                if (socket != null) {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    //send data do user
                    out.println(sensorsData.readFromSensor(virtualConnection.resourceID, virtualConnection.metric));
                }
            }
        }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    synchronized public void addVirtualConnection(int subscriptionID, NetworkAddress clientAddress, String resourceId, String metric){
        Sensor sensor = sensorsData.getSensor(resourceId, metric);
        if(sensor!=null){
            virtualConnections.add(new VirtualConnection(subscriptionID, clientAddress,resourceId,metric));
        }else{
            System.out.println("ClientWriterThread.addVirtualConnection(): Error: cannot add connection, requested sensor does not exist");
        }
    }
    
    public void stopWorking(){
        work = false;
    }

    synchronized void removeVirtualConnection(NetworkAddress networkAddress, String resourceId, String metric) {
        Iterator<VirtualConnection> it = virtualConnections.iterator();
        VirtualConnection vc=null;
        boolean foundMatch = false;
        while(it.hasNext()){
            vc = it.next();
            if(vc.resourceID.equals(resourceId) && vc.metric.equals(metric) && vc.clientNetworkAddress.ipAddress.equals(networkAddress.ipAddress) && vc.clientNetworkAddress.port == networkAddress.port){
                foundMatch = true;
                break;
            }
        }
        if(foundMatch){
            virtualConnections.remove(vc);
        }else{
            System.out.println("ClientWriterThread: couldnt remove virtual connection becouse it dont exist");
        }
    }

    void establishConnection(Socket clientSocket, int id) {
        for(VirtualConnection vc : virtualConnections){
            if(vc.subscribtionID==id){
                vc.clientSocket = clientSocket;
            }
        }
    }
}
